﻿/*
Copyright © 2010 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;

namespace MSBuild.Mercurial.Command
{
    /// <summary>
    /// Provides the existing implementation of the <see cref="IParameterBuilder"/>
    /// interface.
    /// </summary>
    internal class ParameterBuilderFactory : IParameterBuilder
    {
        /// <summary>
        /// The unique instance of the class.
        /// </summary>
        private static ParameterBuilderFactory instance;

        /// <summary>
        /// Prevents a default instance of the ParameterBuilderFactory class from being created.
        /// </summary>
        private ParameterBuilderFactory()
        {
            this.Builders = new IParameterBuilder[]
            {
                new ChoiceParameterBuilder(),
                new DateParameterBuilder(),
                new ItemsParameterBuilder(),
                new StringParameterBuilder(),
            };
        }

        /// <summary>
        /// Gets the unique instance of the class.
        /// </summary>
        public static ParameterBuilderFactory Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ParameterBuilderFactory();
                }

                return instance;
            }
        }

        /// <summary>
        /// Gets the existing <see cref="IParameterBuilder"/> implementations.
        /// </summary>
        public IParameterBuilder[] Builders { get; private set; }

        /// <summary>
        /// Tests if a property is managed by one of the existing
        /// <see cref="IParameterBuilder"/> implementations.
        /// </summary>
        /// <param name="propertyType">
        /// The type of the property.
        /// </param>
        /// <returns>
        /// <c>true</c> if the factory can work with the property.
        /// </returns>
        public bool CanBuild(Type propertyType)
        {
            foreach (IParameterBuilder builder in this.Builders)
            {
                if (builder.CanBuild(propertyType))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Builds the parameter associated with a value.
        /// </summary>
        /// <param name="parameterPrefix">
        /// The prefix used for the parameter. If empty, the builded
        /// parameter is the default parameter.
        /// </param>
        /// <param name="propertyType">
        /// The type of the property.
        /// </param>
        /// <param name="propertyValue">
        /// The current value of the property. Can be <c>null</c>.
        /// </param>
        /// <returns>
        /// The string used to represent the current property in the
        /// Mercurial command.
        /// </returns>
        /// <exception cref="NotSupportedException">
        /// The conversion of the current value is not supported by
        /// the existing <see cref="IParameterBuilder"/> implementations.
        /// </exception>
        public string Build(string parameterPrefix, Type propertyType, object propertyValue)
        {
            foreach (IParameterBuilder builder in this.Builders)
            {
                if (builder.CanBuild(propertyType))
                {
                    return builder.Build(parameterPrefix, propertyType, propertyValue);
                }
            }

            throw new NotSupportedException(Error.ParameterBuilderCantBuild);
        }
    }
}
